{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# String Manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Exercise 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a = \"To\"\n",
    "b = \"Be\"\n",
    "c = \"Or\"\n",
    "d = \"Not\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "a + \" \" + b + \" \" + c + \" \" + d + \" \" + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "\" \".join((a, b, c, d, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "quote = \" \".join((a, b, c, d, b))\n",
    "quote.upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "quote.lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "quote"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Exercise 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "csv = '''\\\n",
    "Bob,Silent,1 Pay Your Taxes St.,Holtsville, NY, 00501\n",
    "George,Curious,0 0th Ave.,New York, NY,10005\n",
    "\"Jay \"\"Party Time\"\"\",Grizwald,99 Main St.,Paris, TX,75324\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "csv.split(\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "parts = csv.split(\"\\n\")\n",
    "for i in range(3):\n",
    "    print(parts[i].split(\",\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "Better to directly iterate on the tuple produced by `split`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for row in csv.split(\"\\n\"):\n",
    "    print(row.split(\",\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for row in csv.split(\"\\n\"):\n",
    "    zipcode = row.split(\",\")[-1]\n",
    "    print(zipcode.strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from numpy import exp, pi, sqrt\n",
    "\n",
    "print(f\"{pi:0.2f}\")\n",
    "print(f\"{pi:0.4f}\")\n",
    "print(f\"{pi:0.5e}\")\n",
    "print(f\"{pi:> 15.6f}\")\n",
    "\n",
    "e = exp(1)\n",
    "phi = (1 + sqrt(5)) / 2\n",
    "print(f\"{pi:>20.6f}{e:>20.6f}{phi:>20.6f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Exercise 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "s = \"1,1.0,cherry,-4.321,horse,1.3+2.96j\"\n",
    "numbers = re.compile(\"[+-]?\\d+(?:\\.\\d+)?[+]?(\\d+)?(?:\\.\\d+)?[j]?\")\n",
    "values = []\n",
    "for v in numbers.finditer(s):\n",
    "    val = v.group()\n",
    "    print(val)\n",
    "    values.append(val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def conv(val):\n",
    "    try:\n",
    "        out = int(val)\n",
    "    except:\n",
    "        try:\n",
    "            out = float(val)\n",
    "        except:\n",
    "            try:\n",
    "                out = complex(val)\n",
    "            except:\n",
    "                out = val\n",
    "                print(\"Not a number\")\n",
    "    print(type(out))\n",
    "    return out\n",
    "\n",
    "\n",
    "for val in s.split(\",\"):\n",
    "    print(conv(val))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Exercise 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "example = \"\"\"\\\n",
    "John's favorite subject is econometrics. He always liked math\n",
    "and especially took to stats. His favorite teacher is Prof. Gauss,\n",
    "and his least favorite is Prof. Bayes. Sarah was stronger at\n",
    "physics than at statistics. She preferred to the purity of objects\n",
    "in a vacuum to causal inference. Of course, she was happy to\n",
    "apply statistics in her dissertation where she could measure\n",
    "the position of the falling feature to the nanometer, and so it\n",
    "was as if she had no measurement error.\n",
    "\"\"\"\n",
    "\n",
    "import re\n",
    "\n",
    "mod = re.sub(\"\\sHe|\\sShe\", \" Ze\", example)\n",
    "mod = re.sub(\"\\she|\\sshe\", \" ze\", mod)\n",
    "mod = re.sub(\"\\sHis|\\sHer\", \" Zir\", mod)\n",
    "mod = re.sub(\"\\shis|\\sher\", \" zir\", mod)\n",
    "print(mod)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
